package com.yksj.healthtalk.ui.dictionary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.dictionary.DictionaryExpandListFragment.DictionaryListItemClickListener;

/**
 * 
 * 检查
 * @author zhao
 *
 */
public class DictionaryCheckActivity extends BaseFragmentActivity implements OnClickListener,DictionaryListItemClickListener{
	RadioGroup mTitleRadioGroup;
	LinearLayout mIndicatorLayout;
	String titleName;
	String type;
	private int mToNextIndex;//到下一个tabl的位置
	
	List<String> titleList = new ArrayList<String>();//title文字
	Map<String,String> map = new HashMap<String, String>();//缓存数据
	
	/**
	 * 监听返回
	 */
	class MJsonHttpResponseHandler extends JsonHttpResponseHandler{
		private String mType;
		private String mTitleName;
		
		public MJsonHttpResponseHandler(String type,String titleName){
			super(DictionaryCheckActivity.this);
			this.mType = type;
			this.mTitleName = titleName;
		}
		
		public void onSuccess(int statusCode, org.json.JSONObject response) {
			super.onSuccess(statusCode, response);
			if(!isFinishing()){
				onParseResultData(response,mType,mTitleName);
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.dictionary_check_content_layout);
		
		type = getIntent().getStringExtra("parame");//类型
		titleName = getIntent().getStringExtra("title");//
		
		initView();
		
		doQueryData(type,null);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title1://第一个点击
			mToNextIndex = 0;
			doQueryData(type,null);
			break;
		case R.id.title2://第二个点击
			mToNextIndex = 1;
			doQueryData(type,null);
			break;	
		}
	}
	
	/**
	 * 列表点击了
	 */
	@Override
	public void onItemClick(String name, String code,int index) {
		++mToNextIndex;
		doQueryData(code,name);
	}
	
	private void initView(){
		initTitle();
		
//		titleLeftBtn.setBackgroundResource(R.drawable.doctorbook_back);
		titleLeftBtn.setOnClickListener(this);
		mIndicatorLayout = (LinearLayout)findViewById(R.id.title_indicator);//title文字
		titleTextV.setText(titleName);
		
		onCreateTabTitle();
	}

	/**
	 * 创建title标签
	 */
	private void onCreateTabTitle(){
		initTabLable();
		
		int size  = titleList.size();
		LayoutInflater inflater = LayoutInflater.from(this);
		if(size == 2){
			mTitleRadioGroup = (RadioGroup)inflater.inflate(R.layout.dictionary_two_title,null);
			mTitleRadioGroup.findViewById(R.id.title1).setOnClickListener(this);
			mIndicatorLayout.addView(mTitleRadioGroup);
		}else if(size == 3){
			mTitleRadioGroup = (RadioGroup)inflater.inflate(R.layout.dictionary_three_title,null);
			mIndicatorLayout.addView(mTitleRadioGroup);
			mTitleRadioGroup.findViewById(R.id.title1).setOnClickListener(this);
		}
		//为title设置文字
		if(mTitleRadioGroup != null){
			while(size > 0){
				size--;
				RadioButton radioButton = (RadioButton)mTitleRadioGroup.getChildAt(size);
				radioButton.setText(titleList.get(size));
			}
		}
		
	}
	
	/**
	 * 初始化lable
	 */
	private void initTabLable(){
		titleList.add(getString(R.string.level1));
		titleList.add(getString(R.string.level2));
		titleList.add(getString(R.string.level3));
	}
	


	/**
	 *  解析数据
	 * @param jsonObject
	 * @param index tab要选择的位置
	 */
	private void onParseResultData(JSONObject jsonObject,String type,String titleName){
		try{
			Object object = jsonObject.get("data");
			if(object instanceof JSONArray){
				onChangeTable(jsonObject.toString());
				map.put(type, jsonObject.toString());
			}else{
				Intent intent = new Intent(this,DictionaryContentActivity.class);
				intent.putExtra("result",jsonObject.toString());
				intent.putExtra("title",titleName);
				startActivityForResult(intent,1);	
			}
		}catch(JSONException e){
			e.printStackTrace();
		}
	}
	
	private void updateTablLable(String name){
		//改变table
		RadioButton box = (RadioButton)mTitleRadioGroup.getChildAt(mToNextIndex);
		box.setChecked(true);
	}
	
	/**
	 * 改变table
	 */
	private void onChangeTable(String content){
		FragmentManager fg = getSupportFragmentManager();
		Bundle bundle = new Bundle();
		bundle.putString("parame",content);
		Fragment fragment = Fragment.instantiate(this,CheckExpandListFragment.class.getName(),bundle);
		FragmentTransaction ft = fg.beginTransaction();
		ft.replace(R.id.fragment,fragment);
		ft.commitAllowingStateLoss();
		
		updateTablLable(null);
	}

	/**
	 * 根据级别查询数据
	 * @param type
	 */
	private void doQueryData(String type,String titleName){
		if(map.containsKey(type)){
			String content = map.get(type);
			onChangeTable(content);
		}else{
			HttpRestClient.doHttpQueryInspectionsServlet(type,new MJsonHttpResponseHandler(type,titleName));
		}
	}
	
	/**
	 * 
	 * 获得选择的位置
	 * @return
	 */
	private int getTabIndex(){
		int id = mTitleRadioGroup.getCheckedRadioButtonId();
		switch(id){
			case R.id.title1:
				id = 0;
			break;
			case R.id.title2:
				id = 1;
				break;
			case R.id.title3:
				id = 2;
				break;
			default:
				id = 0;
				break;
		}
		return id;
	}
}
